//
//  LVRegisterViewController.m
//  LEVE
//
//  Created by 王若聪 on 2017/2/22.
//  Copyright © 2017年 dashuju. All rights reserved.
//

#import "LVRegisterViewController.h"

#import "NTESGlobalMacro.h"
#import "SVProgressHUD.h"
#import "UIView+Toast.h"
#import "NSString+Addition.h"
#import "UIButton+Addition.h"

@interface LVRegisterViewController ()
@property (nonatomic, strong)UILabel      *statesLabel;
@property (nonatomic, strong)UIButton     *statesSelectBtn;
@property (nonatomic, strong)UILabel      *phoneNumCodeLabel;
@property (nonatomic, strong)UITextField  *phoneNumTextFiled;
@property (strong, nonatomic) UIButton    *phoneNumTextFiledClearTextBtn;

@property (nonatomic, strong)UILabel      *descriptionLabel;


@property (nonatomic, strong)UIButton     *nextStepBtn;

@property (nonatomic, copy)NSString     *phoneNumString;

@end

@implementation LVRegisterViewController

- (instancetype)init{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.statesLabel = [[UILabel alloc]init];
    self.statesLabel.text = @"国家或地区";
    self.statesLabel.font = [UIFont systemFontOfSize:18];
    self.statesLabel.textColor  = UIColorFromRGB(0x9B9B9B);
    //    self.statesLabel.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.statesLabel];
    [self.statesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@53);
        make.top.equalTo(@169);
        make.height.equalTo(@20);
        make.width.equalTo(@120);
    }];
    
    self.statesSelectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.statesSelectBtn setTitle:@"中国" forState:UIControlStateNormal];
    self.statesSelectBtn.titleLabel.font = [UIFont systemFontOfSize:18.f];
    [self.statesSelectBtn setTitleColor:UIColorFromRGB(0x7193CB) forState:UIControlStateNormal];
    //    self.statesSelectBtn.backgroundColor = [UIColor yellowColor];
    [self.statesSelectBtn addTarget:self action:@selector(statesSelectBtnTouched) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.statesSelectBtn];
    [self.statesSelectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.statesLabel.mas_top);
        make.height.equalTo(@20);
        make.right.equalTo(self.view.mas_right).offset(-53);
    }];
    
    UIView *bottomLineView = [[UIView alloc]init];
    bottomLineView.backgroundColor = UIColorFromRGB(0xD7D7D7);
    [self.view addSubview:bottomLineView];
    [bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.statesLabel.mas_bottom).offset(8);
        make.height.equalTo(@2);
        make.left.equalTo(self.statesLabel.mas_left);
        make.right.equalTo(self.statesSelectBtn.mas_right);
    }];
    
    self.phoneNumCodeLabel = [[UILabel alloc]init];
    self.phoneNumCodeLabel.text = @"+86";
    self.phoneNumCodeLabel.font = [UIFont systemFontOfSize:18];
    self.phoneNumCodeLabel.textColor  = UIColorFromRGB(0x9B9B9B);
    //    self.statesLabel.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.phoneNumCodeLabel];
    [self.phoneNumCodeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.statesLabel.mas_left);
        make.top.equalTo(self.statesLabel.mas_bottom).offset(35);
        make.height.equalTo(@20);
        make.width.equalTo(@60);
    }];
    
    self.phoneNumTextFiled = [[UITextField alloc]init];
    self.phoneNumTextFiled.placeholder = @"手机号码";
    self.phoneNumTextFiled.tintColor = [UIColor grayColor];
    [self.view addSubview:self.phoneNumTextFiled];
    self.phoneNumTextFiled.keyboardType = UIKeyboardTypeNumberPad;
    [self.phoneNumTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@20);
        make.left.equalTo(self.phoneNumCodeLabel.mas_left).offset(65);
        make.right.equalTo(self.view).offset(-50);
        make.top.equalTo(self.phoneNumCodeLabel.mas_top);
    }];
    [self.phoneNumTextFiled setValue:[UIFont systemFontOfSize:18] forKeyPath:@"_placeholderLabel.font"];
    [self.phoneNumTextFiled setValue:UIColorFromRGBA(0xA1A7AE, .6f) forKeyPath:@"_placeholderLabel.textColor"];
    
    UIView *bottomLineView2 = [[UIView alloc]init];
    bottomLineView2.backgroundColor = UIColorFromRGB(0xD7D7D7);
    [self.view addSubview:bottomLineView2];
    [bottomLineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.phoneNumCodeLabel.mas_bottom).offset(8);
        make.height.equalTo(@2);
        make.left.equalTo(self.phoneNumCodeLabel.mas_left);
        make.right.equalTo(self.phoneNumTextFiled.mas_right);
    }];
    
    self.descriptionLabel = [[UILabel alloc]init];
    self.descriptionLabel.text = @"注册意味你同意服务条款与隐私条款，包括cookie使用政策，其他用户可以通过你提供的手机号码搜寻到你。";
    self.descriptionLabel.numberOfLines = 0;
    self.descriptionLabel.font = [UIFont systemFontOfSize:11];
    self.descriptionLabel.textColor  = UIColorFromRGB(0xBABABA);
    //    self.descriptionLabel.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:self.descriptionLabel];
    [self.descriptionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.phoneNumCodeLabel.mas_left);
        make.right.equalTo(self.phoneNumTextFiled.mas_right);
        make.top.equalTo(self.phoneNumCodeLabel.mas_bottom).offset(20);
        make.height.equalTo(@80);
    }];
    
    
    
    self.nextStepBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.nextStepBtn setTitle:@"下一步" forState:UIControlStateNormal];
    self.nextStepBtn.titleLabel.font = [UIFont systemFontOfSize:22.f];
    [self.nextStepBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [self.nextStepBtn setBackgroundImage:[self.nextStepBtn buttonImageWithColor:UIColorFromRGB(0xD0CFCB)] forState:UIControlStateNormal];
    [self.nextStepBtn setBackgroundImage:[self.nextStepBtn buttonImageWithColor:UIColorFromRGB(0xFFB32C)] forState:UIControlStateHighlighted];
    
    [self.nextStepBtn addTarget:self action:@selector(nextStepBtnTouched) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.nextStepBtn];
    [self.nextStepBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@56);
        make.left.equalTo(self.descriptionLabel.mas_left);
        make.right.equalTo(self.descriptionLabel.mas_right);
        make.top.equalTo(self.descriptionLabel.mas_bottom).offset(30);
    }];
    self.nextStepBtn.layer.cornerRadius = 28;
    self.nextStepBtn.layer.masksToBounds = YES;
    self.nextStepBtn.enabled = NO;
    
}

- (void)statesSelectBtnTouched{
    
    
}

- (void)nextStepBtnTouched{
    
    
    
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if ([string isEqualToString:@"\n"]) {
        [self nextStepBtnTouched];
        return NO;
    }
    return YES;
}

- (void)textFieldDidChange:(NSNotification*)notification{
    
    self.phoneNumString = self.phoneNumTextFiled.text;
    if ([self.phoneNumTextFiled.text length])
    {
        self.nextStepBtn.enabled = YES;
        [self.nextStepBtn setBackgroundImage:[self.nextStepBtn buttonImageWithColor:UIColorFromRGB(0xFFCD00)] forState:UIControlStateNormal];
        
    }else{
        self.nextStepBtn.enabled = NO;
        [self.nextStepBtn setBackgroundImage:[self.nextStepBtn buttonImageWithColor:UIColorFromRGB(0xD0CFCB)] forState:UIControlStateNormal];
    }
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if ([self.phoneNumTextFiled.text length])
    {
        self.nextStepBtn.enabled = YES;
        [self.nextStepBtn setBackgroundImage:[self.nextStepBtn buttonImageWithColor:UIColorFromRGB(0xFFCD00)] forState:UIControlStateNormal];
        
    }else{
        self.nextStepBtn.enabled = NO;
        [self.nextStepBtn setBackgroundImage:[self.nextStepBtn buttonImageWithColor:UIColorFromRGB(0xD0CFCB)] forState:UIControlStateNormal];
    }
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    [self.phoneNumTextFiled resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
